import { Controller, Get, Param, Res } from '@nestjs/common';
import { Response } from 'express';
import * as path from 'path';
import * as fs from 'fs';

@Controller('download')
export class DownloadController {
  private readonly outputBasePath = process.env.projectSaveZipPath || '../../Output';

  @Get(':filename')
  downloadFile(@Param('filename') filename: string, @Res() res: Response) {
    const filePath = path.join(this.outputBasePath, filename);
    console.log('downloadFile', filePath);
    
    if (fs.existsSync(filePath)) {
      return res.download(filePath);
    } else {
      res.status(404).send('File not found');
    }
  }
}
